Hiding Empty Paragraphs Using :empty in CSS
You can use the :empty pseudo-class in CSS to select and hide elements that contain no child elements or text nodes. This is useful for cleaning up unwanted empty paragraphs in dynamically generated content or CMS outputs.
:empty matches elements that have no children, including text nodes or whitespace.
It can be used to hide empty elements like <p>, <div>, or <span>.
Whitespace or line breaks inside an element will prevent :empty from matching.
In this example, the second paragraph (<p></p>) is hidden because it’s empty. The :empty pseudo-class applies display: none; only to paragraphs that have no content or children.
Use :empty to clean up layouts affected by unwanted empty elements.
Ensure that no invisible characters (like spaces or line breaks) exist inside empty tags if you want them to be hidden.
Avoid using it for layout-critical elements; remove empty tags in markup when possible.
Test across browsers, as whitespace handling may differ in some editors or CMS outputs.
How would you hide a <p> tag that’s completely empty using only CSS?
What happens if a <p> tag has a single space inside — will :empty still work? How do you fix it?
Our content CMS sometimes renders <p> tags with just line breaks or non-breaking spaces — users see blank gaps. How would you debug and fix this?
A designer says the page looks broken because of invisible empty paragraphs. You’ve tried :empty but it’s not working. What’s your next step?
We have a rich-text editor that generates hundreds of empty <p> tags on user submissions — how would you design a performant, maintainable solution to hide them without impacting layout shifts or accessibility?
In a large component library, empty <p> tags are causing inconsistent spacing across pages. How would you enforce consistent styling without breaking existing content?
We’re migrating from a legacy CMS that outputs tons of empty <p> tags — should we fix this at the CSS layer, the template layer, or the data layer? What are the long-term maintenance tradeoffs?
How would you architect a cross-team standard for handling empty semantic elements across 50+ micro-frontends, balancing visual cleanliness, accessibility, and developer ergonomics?